//////////////////////////GLOBAL VARIABLES////////////////////////////////BEGIN
var pageScriptLoaded = false;
var dataCommunication = new DataCommunication();
var pageController = new PageController();
//////////////////////////GLOBAL VARIABLES//////////////////////////////////END
////////////////////////HEADER page/////////////////////////////////////////////
/**
 * Loaded when document ready.
 */
$(function() {
    // load page script
    loadPageScript();
    // start timer to hand server event
    dataCommunication.handleServerEvent(handleServerEvent, handleServerClosed);
});

/**
 * Load all script of page.
 */
function loadPageScript() {
    // do not load if it loaded already
    if (pageScriptLoaded) {
            return;
    }
    // log info
    Utils.log(LogType.INFO, 'Load page script BEGIN!');
    // load page script
    pageController.loadPage();
    // turn on page script loaded.
    pageScriptLoaded = true;	
    // log info
    Utils.log(LogType.INFO, 'Load page script END!');
}

/**
 * Handles server event received.
 * 
 * @param json
 *            the json data of server event received.
 */
function handleServerEvent(json) {
    Utils.log(LogType.INFO, "Server event received: ", json);
    pageController.notifyNewReceivedData(json.event.charCodeAt(0), json.data);
}

function handleServerClosed() {
    Utils.log(LogType.WARNING, "The weather box is unreachable!");
    Utils.showInformDialog("The server is unreachable", "Server is unreachable, you can not interact with your server!");
}

